package com.wyix.controller;

import com.wyix.domain.BorrowRecord;
import com.wyix.service.BorrowService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wangdx
 * @since 2025/8/26
 */
@RestController
@RequestMapping("/borrows")  // 接口前缀：/book-api/borrows
@RequiredArgsConstructor
public class BorrowController {
    private final BorrowService borrowService;

    /**
     * 借阅图书
     */
    @PostMapping
    public ResponseEntity<BorrowRecord> borrowBook(
            @RequestParam Long userId,
            @RequestParam Long bookId) {
        BorrowRecord record = new BorrowRecord();
        record.setUserId(userId);
        record.setBookId(bookId);
        return ResponseEntity.ok(record);
    }

    /**
     * 归还图书
     */
    @PutMapping("/{recordId}/return")
    public ResponseEntity<BorrowRecord> returnBook(@PathVariable Long recordId) {
        BorrowRecord record = borrowService.returnBook(recordId);
        return ResponseEntity.ok(record);
    }

    /**
     * 查询用户借阅记录
     */
    @GetMapping("/user/{userId}")
    public ResponseEntity<List<BorrowRecord>> getUserBorrowRecords(@PathVariable Long userId) {
        List<BorrowRecord> records = borrowService.getUserBorrowRecords(userId);
        return ResponseEntity.ok(records);
    }
}
